
$(document).ready(function() {
	$('body').css("background-size",screen.width+"px "+screen.height+"px");
	// Navigation menu
	$('ul#navigation').superfish({ 
		delay:       1000,
		animation:   {opacity:'show',height:'show'},
		speed:       'fast',
		autoArrows:  true,
		dropShadows: false
	});
	$('ul#navigation li').hover(function(){
		$(this).addClass('sfHover2');
	},
	function(){
		$(this).removeClass('sfHover2');
	});	



	// Live Search
	$('#search-bar input[name="q"]').liveSearch({url: 'live_search.php?q='});

	//Hover states on the static widgets
	$('.ui-state-default').hover(
		function() { $(this).addClass('ui-state-hover'); }, 
		function() { $(this).removeClass('ui-state-hover'); }
	);



	//Sortable portlets
	$('.sortable .column').sortable({
		cursor: "move",
		connectWith: '.sortable .column',
		dropOnEmpty: false
	});
	$(".column").disableSelection();
	
	//Sidebar only sortable boxes

	$(".side_sort").sortable({
		axis: 'y',
		cursor: "move",
		connectWith: '.side_sort'
	});


	//Close/Open portlets
	$(".portlet-header").hover(function() {
		$(this).addClass("ui-portlet-hover");
	},

	function(){
		$(this).removeClass("ui-portlet-hover");
	});

	$(".portlet-header .ui-icon").click(function() {
		$(this).toggleClass("ui-icon-circle-arrow-n");
		$(this).parents(".portlet:first").find(".portlet-content").toggle();
	});

	// Sidebar close/open (with cookies)

	function close_sidebar() {
		$("#sidebar").addClass('closed-sidebar').animate({"speed":"1500"});
		//$("#sidebar").animate("speed",500);
		$("#page_wrapper #page-content #page-content-wrapper").addClass("no-bg-image wrapper-full");
		$("#open_sidebar").show();
		$("#close_sidebar, .hide_sidebar").hide();
	}

	function open_sidebar() {
		$("#sidebar").animate(1500).removeClass('closed-sidebar');
		//$("#sidebar").removeClass('closed-sidebar').animate({"speed":"500"});
		//$("#page_wrapper #page-content #page-content-wrapper").removeClass("no-bg-image wrapper-full");
		$("#page_wrapper #page-content #page-content-wrapper").addClass("no-bg-image wrapper-full");
		$("#open_sidebar").hide();
		$("#close_sidebar, .hide_sidebar").show();
		$("#sidebar .ui-accordion-content").attr('style','height:400px');
		$("#sidebar .ui-accordion-content").css({height:'400px'});
		}

	$('#close_sidebar').click(function(){
    //$('.btn ui-state-default full-link ui-corner-all').toggle();
		close_sidebar();
		//$(".content-box").css("width","96%");

		//$(".inner-page-title").css("width","98%");

		var sidebarHeight = $("#sidebar").height();
		//alert(sidebarHeight);
		$("#page-content-wrapper").css({"minHeight" : sidebarHeight });
		/*if(jQuery.browser.safari) {
		    location.reload();
		}*/
		$.cookie('sidebar', 'closed' );
			$(this).addClass("active");
	});

	$('#open_sidebar').click(function(){
		open_sidebar();
		//$(".content-box").css("width","98%");
		//$(".inner-page-title").css("width","100%");
			var sidebarHeight = $("#sidebar").height();
			//salert(sidebarHeight);
	$("#page-content-wrapper").css({"minHeight" : sidebarHeight });
/*
		if(jQuery.browser.safari) {
		    location.reload();
		}*/
		$.cookie('sidebar', 'open' );
	});

	var sidebar = $.cookie('sidebar');
		if (sidebar == 'closed') {
			close_sidebar();
	    };
		if (sidebar == 'open') {
			open_sidebar();
	    };

	/* Tooltip */
	$(function() {
		$('.tooltip').tooltip({
			track: true,
			delay: 0,
			showURL: false,
			showBody: " - ",
			fade: 250
			});
		});


	/* Theme changer - set cookie */

    $(function() {
        $('a.set_theme').click(function() {
           	var theme_name = $(this).attr("id");
			$('body').append('<div id="theme_switcher" />');
			$('#theme_switcher').fadeIn('fast');
			setTimeout(function () { 
				$('#theme_switcher').fadeOut('fast');
			}, 1000);
			setTimeout(function () { 
			$("link[title='style']").attr("href","css/themes/" + theme_name + "/ui.css");
			}, 500);			
			$.cookie('theme', theme_name );
			$('a.set_theme').removeClass("active");
			$(this).addClass("active");
        });

		var theme = $.cookie('theme');
       //alert(theme);
		$("a.set_theme[id="+ theme +"]").addClass("active");
		
		if (theme == 'black_rose') {
	        $("link[title='style']").attr("href","css/themes/black_rose/ui.css");
	    };

		if (theme == 'gray_standard') {
	        $("link[title='style']").attr("href","css/themes/gray_standard/ui.css");
	    };
		
		if (theme == 'gray_lightness') {
	        $("link[title='style']").attr("href","css/themes/gray_lightness/ui.css");
	    };

		if (theme == 'blueberry') {
	        $("link[title='style']").attr("href","css/themes/blueberry/ui.css");
	    };

		if (theme == 'apple_pie') {
	        $("link[title='style']").attr("href","css/themes/apple_pie/ui.css");
	    };

		if(theme == 'blue_sky'){
			$("link[title='style']").attr("href","css/themes/blue_sky/ui.css");
		};		

		if(theme == 'turquoise'){
			$("link[title='style']").attr("href","css/themes/turquoise/ui.css");
		};

		if (theme == 'salmon') {
	        $("link[title='style']").attr("href","css/themes/salmon/ui.css");
	    };
    });



    



	/* Layout option - Change layout from fluid to fixed with set cookie */

    $(function() {

		$('.layout-options a').click(function(){
			var lay_id = $(this).attr("id");
			$('body').attr("class",lay_id);
			$("#page-layout, #page-header-wrapper, #sub-nav,#page_wrapper").addClass("fixed");
			$.cookie('layout', lay_id );
			$('.layout-options a').removeClass("active");
			$(this).addClass("active");
		})
	    var lay_cookie = $.cookie('layout');
     // alert(lay_cookie);
		$(".layout-options a[id="+ lay_cookie +"]").addClass("active");
		if (lay_cookie == 'layout100') {
			$('body').attr("class","");
			$("#page-layout, #page-header-wrapper, #sub-nav","#page-header","#navigation","#page_wrapper").removeClass("fixed");
	    };
		if (lay_cookie == 'layout90') {
			// alert(lay_cookie);
			$('body').attr("class","layout90");
			$("#page-layout, #page-header-wrapper, #sub-nav","#page-header","#navigation","#page_wrapper").addClass("fixed");
	    };

		if (lay_cookie == 'layout75') {
 //alert(lay_cookie);
			$('body').attr("class","layout75");
			$("#page-layout, #page-header-wrapper, #sub-nav","#page-header","#navigation","#page_wrapper").addClass("fixed");
	    };
		if (lay_cookie == 'layout980') {
			$('body').attr("class","layout980");
			$("#page-layout, #page-header-wrapper, #sub-nav","#page-header","#navigation","#page_wrapper").addClass("fixed");
	    };
		if (lay_cookie == 'layout1280') {
			$('body').attr("class","layout1280");
			$("#page-layout, #page-header-wrapper, #sub-nav","#page-header","#navigation","#page_wrapper").addClass("fixed");
		};
		if (lay_cookie == 'layout1400') {
			$('body').attr("class","layout1400");
			$("#page-layout, #page-header-wrapper, #sub-nav","#page-header","#navigation","#page_wrapper").addClass("fixed");
	    };
		if (lay_cookie == 'layout1600') {
			$('body').attr("class","layout1600");
			//$('#bdylay').attr("class","layout1600");
			$("#page-layout, #page-header-wrapper, #sub-nav","#page-header","#navigation","#page_wrapper").addClass("fixed");
	    };

    });

	// Dialog			

	$('#dialog').dialog({
		autoOpen: false,
		width: 600,
		bgiframe: false,
		modal: true,
			zIndex: 9999,
		buttons: {
			"Ok": function() { 
				$(this).dialog("close"); 
			}, 
			"Cancel": function() { 
				$(this).dialog("close");
			} 
		}
	});
	$('#dialog2').dialog({
		autoOpen: false,
		width: 800,
		bgiframe: false,
		modal: true,
		zIndex: 9999,
		buttons: {
			"Done": function() { 
				$(this).dialog("close"); 
			}
		}
	});
	$('#dialog2_link').click(function(){
		$('#dialog2').dialog('open');
		return false;
	});	
	
	// Same height
	var sidebarHeight = $("#sidebar").height();
	$("#page-content-wrapper").css({"minHeight" : sidebarHeight });



	// Simple drop down menu

	var myIndex, myMenu, position, space=20;
	$("div.sub").each(function(){
		$(this).css('left', $(this).parent().offset().left);
		$(this).slideUp('fast');
		});
	
	$(".drop-down li").hover(function(){
		$("ul",this).slideDown('fast');
		//get the index, set the selector, add class
		myIndex = $(".main1").index(this);
		myMenu = $(".drop-down a.btn:eq("+myIndex+")");
	}, function(){
		$("ul",this).slideUp('fast');
	});
	
	if($("#redirect").length != 0)
 		setTimeout('redirect("' + $("#redirect").attr("url") + '")', $("#redirect").attr("timeout"));	

	$("input[checker=true]").click(function(){
		var check = $("input[cnode=" + $(this).attr('node') + "]");
		check.attr('checked', $(this).is(':checked'));
	});
	$(window).resize(function(){
		if($('#page-content-wrapper').offset().left < 240){
			$('#logo').animate({opacity: 0.4}, 500 ).animate({top:'-200px'}, 100 );
			$('#logo2').animate({opacity: 1}, 500 );
		}else{
			$('#logo').animate({opacity: 1}, 500 ).animate({top:'2px'}, 100 );
			$('#logo2').animate({opacity: 0}, 500 );
		}
	});
	$(window).trigger('resize');
	
	
 
});
function redirect(url){
	window.location = url;
}